home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 3239 / 3239.xpi / chrome / inbasicph.jar / content / jStatusOverlay.js < prev    next >
Text File  |  2010-01-19  |  31KB  |  815 lines

  1. var PhProxy_object ;
  2.  
  3. //create desire url for phproxy or glype 
  4. var PhProxy_url_creator = {
  5.     addressbar_view_mode : {
  6.         bin2hexOneDig : function (input) {
  7.             if(input=='0000')return '0';
  8.             if(input=='00')  return '0';
  9.             if(input=='0001')return '1';
  10.             if(input=='01')  return '1';
  11.             if(input=='0010')return '2';
  12.             if(input=='10')  return '2';
  13.             if(input=='0011')return '3';
  14.             if(input=='11')  return '3';
  15.             if(input=='0100')return '4';
  16.             if(input=='0101')return '5';
  17.             if(input=='0110')return '6';
  18.             if(input=='0111')return '7';
  19.             if(input=='1000')return '8';
  20.             if(input=='1001')return '9';
  21.             if(input=='1010')return 'a';
  22.             if(input=='1011')return 'b';
  23.             if(input=='1100')return 'c';
  24.             if(input=='1101')return 'd';
  25.             if(input=='1110')return 'e';
  26.             if(input=='1111')return 'f';
  27.         },        
  28.         bin2hex : function (input) {
  29.             return ( this.bin2hexOneDig(input.substr(0,2)) + 
  30.                      this.bin2hexOneDig(input.substr(2,4)) + 
  31.                      this.bin2hexOneDig(input.substr(6,4)) )
  32.         },
  33.         get : function() {
  34.             if(PhProxy_prefs.read_str('PrType')=="2")  //glype mode 1
  35.                 return 'b5/';    
  36.             if(PhProxy_prefs.read_str('PrType')=="3")  //glype mode 2
  37.                 return '&b=29';
  38.             else{
  39.                 var sCntr=PhProxy_prefs.read_str('Contr').split('|');                
  40.                 var OutPut=sCntr[0]+sCntr[1]+sCntr[2]+sCntr[3]+sCntr[4]+(PhProxy_prefs.read_bool('useROT13') ? '10' : '01')+sCntr[5]+sCntr[6]+sCntr[7];
  41.                 if(PhProxy_prefs.read_str('PrType')=="1")
  42.                     return this.bin2hex(OutPut);
  43.                 else
  44.                     return OutPut ;
  45.             }
  46.         }
  47.     },    
  48.  
  49.     glype_proxify : function (str){
  50.         str=str.substr(4);                 // remove http
  51.         str=str.replace(/\/.\//g,'/');
  52.         str=str.replace(/\/\//g,'/');
  53.         str=str.replace(/:\//g,'://');
  54.         
  55.         str = PhProxy_Base64.encode(str);        
  56.         str=encodeURIComponent(str); //instead of  $url = rawPh_urlEncode($url);
  57.         
  58.         const sURL=PhProxy_prefs.read_str('curURL');
  59.         
  60.         if(PhProxy_prefs.read_str('PrType')=="2"){
  61.             //chunk_split($url,rand(7,14),'/'))
  62.             tSTR = "";
  63.             for(i=0;i<str.length;i+=8)
  64.                 tSTR+=str.substr(i,8)+'/';
  65.             str = tSTR;
  66.             str = str.replace(/\%/g,'_');
  67.             str = sURL + PhProxy_prefs.read_str('glypeIndex') + "/"+ str + this.addressbar_view_mode.get();
  68.             return str;
  69.         }
  70.         else{    
  71.             str = sURL + PhProxy_prefs.read_str('glypeIndex') + "?u="+ str + this.addressbar_view_mode.get();
  72.             return str;
  73.         }
  74.     },    
  75.     glype_deproxify : function (str){    
  76.         //remove base
  77.         if(PhProxy_prefs.read_str('PrType')=="2"){
  78.             charLocation=str.indexOf(PhProxy_prefs.read_str('glypeIndex'));
  79.             if(charLocation!=-1) str=str.substr(charLocation+10);
  80.         }
  81.         else{
  82.             charLocation=str.indexOf(PhProxy_prefs.read_str('glypeIndex') + '?u=');
  83.             if(charLocation!=-1) str=str.substr(charLocation+13);
  84.             charLocation=str.indexOf('&b=');
  85.             if(charLocation!=-1) str=str.substr(0,charLocation);
  86.         }
  87.         str=str.replace(/\/b[0-9]{1,3}\/(?:f[a-z]{1,10}\/?)?/, '/');
  88.         str=str.replace(/\//g, '');
  89.         str=str.replace(/_/g, '%');
  90.         str=PhProxy_Glype_decode.run(str);
  91.         str="http"+PhProxy_Base64.decode(str, "Glype");
  92.         str=str.replace(/&/g, '&');
  93.         str=str.replace(/ /g, '%20');
  94.         
  95.         return str;
  96.     },
  97.     phproxy_proxify : function(str){
  98.         const sURL=PhProxy_prefs.read_str('curURL');    
  99.         
  100.         if (PhProxy_prefs.read_bool('useROT13'))
  101.             return sURL + PhProxy_prefs.read_str('phpIndex') + '?q=' + PhProxy_Rot13.encode(str)+ '&hl='+this.addressbar_view_mode.get();        
  102.         else        
  103.             return sURL + PhProxy_prefs.read_str('phpIndex') + '?q=' + PhProxy_Base64.encode(str)+ '--&hl='+this.addressbar_view_mode.get();
  104.     },
  105.     phproxy_deproxify : function(str){
  106.         if (PhProxy_prefs.read_bool('useROT13'))
  107.             return PhProxy_Rot13.decode(str);
  108.         else 
  109.             return PhProxy_Base64.decode(str);
  110.     },
  111.     proxify : {  
  112.         phf_check_if_url_is_video_site : function (data) {
  113.             if( !data.match(/[A-Za-z0-9_.\/\?=\:]*keepvid[A-Za-z0-9_.\/\?=\:]*/) &&   //check if url not contains  "keepvid.com"
  114.             
  115.                 ( data.match(/[A-Za-z0-9_.\/\?=\:]*watch[A-Za-z0-9_.\/\?=\:]*/) ||   // Check youtube file infomrat  "www.youtube.com/watch?v=???"  
  116.                   data.match(/[A-Za-z0-9_.\/\?=\:]*\/v\/[A-Za-z0-9_.\/\?=\:]*/) )    // Check youtube file infomrat  "http://www.youtube.com/v/???"
  117.               )
  118.                 return true;
  119.             else
  120.                 return false
  121.         },    
  122.         apply : function (str) {
  123.             //Check URL for Youtube
  124.             if ( PhProxy_prefs.read_bool('youTubeV') &&  this.phf_check_if_url_is_video_site( str ) )
  125.                     str="http://keepvid.com/?url=" + str;
  126.             //Caching; http://ostermiller.org/bookmarklets/cache.html; Copyright (c) 2004-2007 by Stephen Ostermiller
  127.             if ( !this.phf_check_if_url_is_video_site(str) && (PhProxy_prefs.read_str('privacyLevel') == '1' || PhProxy_prefs.read_str('privacyLevel') == '2') ) {
  128.                 switch (PhProxy_prefs.read_str('cacheType')) {
  129.                     case '0':    //Google Cache
  130.                         str = "http://www.google.com/search?q=cache:" + escape(str.replace(/^http\:\/\/(.*)$/,"$1"));
  131.                         break;
  132.                     case '1':    //Wayback Machine
  133.                         str = "http://web.archive.org/web/*/" + str;
  134.                         break;
  135.                     default:    //Coral Cache
  136.                         str = str.replace( /^http\:\/\/([^\/\@]+)\/(?:)/, "http://"+"$1".replace("\:",".")+".nyud.net:8090/");
  137.                 }                
  138.             }
  139.             switch (PhProxy_prefs.read_str('privacyLevel')) {
  140.                 case '0':    //PhProxy or PhProxy + Cache
  141.                 case '2':
  142.                     if (PhProxy_prefs.read_str('PrType')=="2" || PhProxy_prefs.read_str('PrType') == "3")
  143.                         return PhProxy_url_creator.glype_proxify(str);    
  144.                     else
  145.                         return PhProxy_url_creator.phproxy_proxify(str);
  146.                     break;
  147.                 default:    //Cache only
  148.                     return str;
  149.             }
  150.         },
  151.         apply_filter : function (data, type) {
  152.             var sURL = PhProxy_prefs.read_str('curURL');
  153.             //Check url validation
  154.             var test = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/.test(data);
  155.             //
  156.             if (test) {
  157.                 if( type=="proxify" )
  158.                     if( data.match(sURL) && !this.phf_check_if_url_is_video_site( PhProxy_url_creator.depoxify(data) ) ) 
  159.                         return data;
  160.                     else if( data.match(sURL) && this.phf_check_if_url_is_video_site( PhProxy_url_creator.depoxify(data) ) )   // for vedio test on PhProxy sites
  161.                         return this.apply(PhProxy_url_creator.depoxify(data));
  162.                     else return this.apply(data);
  163.                 else
  164.                     return PhProxy_url_creator.depoxify(data);
  165.             }
  166.             else{
  167.                 var stringsBundle = document.getElementById("Ph.Main.Bundle");
  168.                 alert(stringsBundle.getString('Alert5')+' '+data+'\n'+ 
  169.                       stringsBundle.getString('Alert6')+'\n\n'+
  170.                       stringsBundle.getString('Alert9')+'\n\thttp://www.google.com\n'+
  171.                       stringsBundle.getString('Alert10')+'\n\twww.google.com\n\tgoogle.com\n\t...');
  172.                 return false;
  173.             }
  174.         }        
  175.     },
  176.     depoxify : function(str){
  177.         if(PhProxy_prefs.read_str('PrType')=="2" || PhProxy_prefs.read_str('PrType')=="3")
  178.             return this.glype_deproxify(str);    
  179.         else
  180.             return this.phproxy_deproxify(str);    
  181.     }    
  182. }
  183.  
  184. var PhProxy = {
  185.     selected_page_href: null,    //save current url of selected page
  186.     mouse_over_href: null,        //save current url of on mouse over
  187.     statusbar_id:  null,        //save Status bar id
  188.     contextmenu_id: null,        //save contextmenu id
  189.     stringsBundle :  null,
  190.     unkonwn_server: null,    
  191.     
  192.     initial : function() {
  193.         PhProxy_object.statusbar_id = document.getElementById('Ph-Status-Icon');
  194.         PhProxy_object.contextmenu_id = document.getElementById("contentAreaContextMenu");
  195.         //initialize string bundle
  196.         PhProxy_object.stringsBundle = document.getElementById("Ph.Main.Bundle");
  197.         //initialize statusbar
  198.         PhProxy_windows.initial();
  199.         // phv_servers_listener for webpage loads
  200.         gBrowser.addProgressListener(PhProxy_object._URLBar_listener, Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT);
  201.         //Context menu on pupup showing
  202.         PhProxy_object.contextmenu_id.addEventListener("popupshowing", PhProxy_object.on_contextmenu_fire, false);
  203.         //Add servers to statusbar        
  204.         var addServersToStatusbar = function () {
  205.             var addOneServer = function (label, value, dServer) {
  206.                 var menuitem = document.createElement("menuitem");
  207.                 menuitem.setAttribute('label', label);
  208.                 menuitem.setAttribute('value', value);
  209.                 menuitem.setAttribute('onclick', "PhProxy.statusbar_menu(label,value," +  dServer +")");          
  210.                 document.getElementById("Ph.Status.PH7.menupopup").appendChild(menuitem);
  211.             }
  212.             //Add default Server
  213.             addOneServer (this.stringsBundle.getString('dServer'), '', true);
  214.             //Add Seperator
  215.             document.getElementById("Ph.Status.PH7.menupopup").appendChild(document.createElement("menuseparator"));
  216.             //Add other servers
  217.             var sURL = PhProxy_prefs.read_str('sURL').split('|');
  218.             for (var i = 0 ; i < 16 ; i += 2)
  219.               addOneServer (sURL[i], sURL[i+1], false);
  220.         }();
  221.         //Show welcome Screen
  222.             /* MUST PLACE BEFORE default_server.update();*/
  223.             /*MUST PLACE AFTER Add servers to statusbar*/
  224.         PhProxy_object.show_welcome_screen_if_needed();
  225.         //update default server
  226.         PhProxy_object.default_server.update();
  227.         //Addressbar icon
  228.         document.getElementById('urlbar').setAttribute('phproxy-show-icon', PhProxy_prefs.read_str('showAddressbar'));
  229.         //Statusbar radio box
  230.         document.getElementById('Ph-Status-PH8-RadioG1').childNodes[parseInt(PhProxy_prefs.read_str('privacyLevel'))].setAttribute("checked", true);
  231.         document.getElementById('Ph-Status-PH8-RadioG2').childNodes[parseInt(PhProxy_prefs.read_str('cacheType'))].setAttribute("checked", true);
  232.     },
  233.  
  234.     open_in : {
  235.         ioSvc : Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService),
  236.         
  237.         current_tab : function(data){
  238.             var referer = this.ioSvc.newURI(PhProxy_prefs.read_str('curURL'), null, null);    
  239.             gBrowser.loadURI(data,referer);
  240.         },
  241.         new_tab : function(data){
  242.             var referer = this.ioSvc.newURI(PhProxy_prefs.read_str('curURL'), null, null);    
  243.             gBrowser.addTab(data,referer);
  244.         }
  245.     },    
  246.     
  247.     _page_listener : {
  248.         apply : function(){
  249.             window.addEventListener("mouseover", PhProxy_object.correct_statusbar_text, false);
  250.             window.addEventListener("mouseout", PhProxy_object.correct_statusbar_text_distroy, false);
  251.             //window.addEventListener("keypress", PhProxy_object.copy_decode_url_to_clipboard,false);
  252.         },
  253.         remove : function(){
  254.             window.removeEventListener("mouseover", PhProxy_object.correct_statusbar_text, false);
  255.             window.removeEventListener("mouseout",    PhProxy_object.correct_statusbar_text_distroy, false);
  256.             //window.removeEventListener("keypress",    PhProxy_object.copy_decode_url_to_clipboard, false);
  257.         }
  258.     },
  259.  
  260.     _URLBar_listener : {
  261.         QueryInterface: function(aIID){
  262.             if (aIID.equals(Components.interfaces.nsIWebProgressListener) || aIID.equals(Components.interfaces.nsISupportsWeakReference) || aIID.equals(Components.interfaces.nsISupports))
  263.                 return this;
  264.             throw Components.results.NS_NOINTERFACE;
  265.         },
  266.         onLocationChange: function(aProgress, aRequest, aURI){ PhProxy.process_new_URL(aURI);},
  267.         onStateChange: function() {},
  268.         onProgressChange: function() {}, 
  269.         onStatusChange: function() {}, 
  270.         onSecurityChange: function() {}, 
  271.         onLinkIconAvailable: function() {}
  272.     },
  273.     statusbar_menu : function(label, value, isItDServer) {
  274.       if (!isItDServer) {
  275.         PhProxy_prefs.write_str('curURL', label);      
  276.         switch(value){
  277.           case "0.4":    PhProxy_prefs.write_str('PrType', "0"); break;  
  278.           case "Glype1": PhProxy_prefs.write_str('PrType', "2"); break; 
  279.           case "Glype2": PhProxy_prefs.write_str('PrType', "3"); break; 
  280.           default:       PhProxy_prefs.write_str('PrType', "1");
  281.         }
  282.       }
  283.       else {
  284.         PhProxy_prefs.write_str('curURL', PhProxy_prefs.read_str('facURL'));
  285.         PhProxy_prefs.write_str('PrType', PhProxy_prefs.read_str('facType'));
  286.       }
  287.       //Change urlbar text to default text
  288.       gURLBar.value =  content.document.location.href;
  289.       //force proccess like new loaded tab
  290.       var aURL = {spec:null};
  291.       aURL.spec = content.document.location.href;
  292.       PhProxy_object.process_new_URL(aURL);
  293.     },    
  294.     
  295.     terminate : function() {
  296.         gBrowser.removeProgressListener(PhProxy_object._URLBar_listener);
  297.     },
  298.  
  299.     process_new_URL : function(aURI) {
  300.         //for null url
  301.         if(!aURI) {
  302.             PhProxy_object.statusbar_id.setAttribute('status', 'Blue');
  303.             //Remove All Listeners on page
  304.             PhProxy_object._page_listener.remove();
  305.             //Remove notification label
  306.             PhProxy_object.hide_notification_on_unknown_servers();
  307.             //If aURL is null then exit!
  308.             return
  309.         }
  310.         PhProxy_object.selected_page_href = aURI.spec;
  311.         
  312.         if(PhProxy_object.selected_page_href.match(PhProxy_prefs.read_str('curURL'))){
  313.             //Corrent status bar icon color
  314.             PhProxy_object.statusbar_id.setAttribute('status', 'Gray');
  315.             //corrent addressbar url
  316.             var decoded_href = PhProxy_url_creator.depoxify(PhProxy_object.selected_page_href);
  317.             setTimeout(function() {
  318.                 gURLBar.value="PH::  " + decoded_href ;
  319.             }, 100);
  320.             //hide notification
  321.             PhProxy_object.hide_notification_on_unknown_servers();    
  322.             //apply all requird phv_servers_listener for keyboard & mouse tracking            
  323.             PhProxy_object._page_listener.apply();
  324.         }
  325.         else{
  326.             //Search to find new server
  327.             if (PhProxy_object.is_this_vaid_phproxy_server(PhProxy_object.selected_page_href)) 
  328.                 PhProxy_object.show_notification_on_unknown_servers(PhProxy_object.selected_page_href);
  329.             else
  330.                 PhProxy_object.hide_notification_on_unknown_servers();        
  331.             //Corrent status bar icon color
  332.             PhProxy_object.statusbar_id.setAttribute('status', 'Blue');
  333.             //Remove All Listeners on page
  334.             PhProxy_object._page_listener.remove();
  335.         }
  336.     },
  337.     
  338.     on_addressbar_click : function(){
  339.         if(!/^PH:: */.test(gURLBar.value))
  340.             PhProxy.open_secure(gURLBar.value);
  341.         else
  342.             alert(this.stringsBundle.getString('Alert2'));
  343.     },
  344.     
  345.     on_contextmenu_fire : function(){
  346.         const menuitem1 = document.getElementById("context-PH1");
  347.         const menuitem2 = document.getElementById("context-PH2");
  348.         const menuitem3 = document.getElementById("context-PH3");
  349.         const menuitem4 = document.getElementById("context-PH4");
  350.         const menuitem5 = document.getElementById("context-PH5");
  351.  
  352.         if(!(gContextMenu)) return;
  353.         var sURL = typeof(gContextMenu.linkURL) == "function"    ? gContextMenu.linkURL() : gContextMenu.linkURL;
  354.         
  355.         if(menuitem1){
  356.             if(!sURL){
  357.                 menuitem1.hidden = menuitem2.hidden = menuitem3.hidden = menuitem4.hidden = menuitem5.hidden = true;
  358.             }
  359.             else if(sURL.match(PhProxy_prefs.read_str('curURL'))){
  360.                 menuitem1.hidden = menuitem2.hidden = gContextMenu.onLink;                
  361.                 menuitem3.hidden = menuitem4.hidden = menuitem5.hidden = !gContextMenu.onLink;
  362.             }
  363.             else{
  364.                 menuitem1.hidden = menuitem2.hidden = !gContextMenu.onLink;
  365.                 menuitem3.hidden = menuitem4.hidden = menuitem5.hidden = gContextMenu.onLink;
  366.             }
  367.         }
  368.     },
  369.     
  370.     context : function(index){ 
  371.         var sURL = typeof(gContextMenu.linkURL) == 'function'    ? gContextMenu.linkURL() : gContextMenu.linkURL;
  372.         switch(index){
  373.             case 1: 
  374.               PhProxy.open_secure(sURL);    
  375.               break;    
  376.             case 2: 
  377.               PhProxy.open_secure_in_new_tab(sURL);        
  378.               break;
  379.             case 3:
  380.                 data = PhProxy_url_creator.proxify.apply_filter(sURL, "depoxify");
  381.                 if( data ) 
  382.                     PhProxy.open_in.current_tab(data);        
  383.                 break;
  384.             case 4:
  385.                 var data = PhProxy_url_creator.proxify.apply_filter(sURL, "depoxify");
  386.                 if( data ) 
  387.                     PhProxy.open_in.new_tab(data);    
  388.                 break;
  389.             default:
  390.               PhProxy.copy_decode_url_to_clipboard(sURL);
  391.         }
  392.     },
  393.     
  394.     show_welcome_screen_if_needed : function() {
  395.         var em = Components.classes["@mozilla.org/extensions/manager;1"]
  396.                          .getService(Components.interfaces.nsIExtensionManager);
  397.  
  398.         var addon = em.getItemForID("amin.eft_PhProxy@gmail.com");
  399.         var version = addon.version;
  400.         if(PhProxy_prefs.read_str("currentVersion") != version){
  401.             //Force updating default server
  402.             PhProxy_prefs.write_str("lastUpdate", "0 0 0");
  403.             //Force updating servers
  404.             PhProxy_object.update_servers();            
  405.             //Showing welcome screen
  406.             setTimeout(function() {
  407.                 var newTab = getBrowser().addTab('http://inbasic.mozdev.org/root/ext1/home/index.htm');
  408.                 getBrowser().selectedTab = newTab;
  409.             }, 600);
  410.             PhProxy_prefs.write_str("currentVersion", version);
  411.         }
  412.     },
  413.     
  414.     default_server : {    
  415.         _server : {
  416.             statusbar_style:  null,        //save Status bar id
  417.             
  418.             initial : function(){
  419.                 statusbar_style = document.getElementById('Ph-Status-Icon').style;
  420.                 statusbar_style.listStyleImage = "url('chrome://inbasicph/skin/statusu.png')";            
  421.                 //Change status on unknown problems after 2 min
  422.                 setTimeout(function() {
  423.                     statusbar_image = "url('chrome://inbasicph/skin/status.png')";
  424.                 }, 120000);    
  425.             },
  426.             finished : function(data){
  427.                 //Try to correct received data even if it contains more lines
  428.                 data = data.match(/[^\n]*/).toString();
  429.                 if (!data) {
  430.                     PhProxy_windows.show_alert_in_statusbar('Alert8');
  431.                     statusbar_style.listStyleImage = "url('chrome://inbasicph/skin/status.png')";
  432.                     return;
  433.                 }                    
  434.                 identifier=data.substr(0,40);
  435.                 if(identifier=="InBasic>DefualtServer>PhProxy>Code124588"){
  436.                     const temp = data.substr(42).split('|');
  437.                     
  438.                     var nwServer=temp[0];            
  439.  
  440.                     if(PhProxy_prefs.read_str('facURL')==PhProxy_prefs.read_str('curURL')){
  441.                         PhProxy_prefs.write_str('curURL',nwServer);                
  442.                         switch(temp[1]){
  443.                             case "0.4":     PhProxy_prefs.write_str('PrType',"0"); break; 
  444.                             case "Glype1":     PhProxy_prefs.write_str('PrType',"2"); break; 
  445.                             case "Glype2":     PhProxy_prefs.write_str('PrType',"3"); break; 
  446.                             default:          PhProxy_prefs.write_str('PrType',"1");
  447.                         }
  448.                     }
  449.                     PhProxy_prefs.write_str('facURL',nwServer);
  450.                     switch(temp[1]){
  451.                         case "0.4":     PhProxy_prefs.write_str('facType',"0"); break; 
  452.                         case "Glype1":     PhProxy_prefs.write_str('facType',"2"); break; 
  453.                         case "Glype2":     PhProxy_prefs.write_str('facType',"3"); break; 
  454.                         default:          PhProxy_prefs.write_str('facType',"1");
  455.                     }            
  456.                     
  457.                     PhProxy_windows.show_alert_in_statusbar('Alert7');
  458.                 }
  459.                 else{
  460.                     PhProxy_windows.show_alert_in_statusbar('Alert8');
  461.                 }
  462.                 statusbar_style.listStyleImage = "url('chrome://inbasicph/skin/status.png')";
  463.             }
  464.         },
  465.         
  466.         update : function (force, num) { //force: true ; update dServer anyway,  num: 0 ; current server; num=1; next server, num=-1 ; previews server
  467.             if(!force)
  468.                 var force = false;        
  469.             if(!num)
  470.                 var num = 0;
  471.             var is_default_server_update_time = force || function () {
  472.                 //Hidden variables
  473.                 switch (PhProxy_prefs.read_str('dServerUpdate')) {
  474.                 case '1':    //case 1: update per day
  475.                     var today = new Date();
  476.                     var year = today.getYear();
  477.                     var ymd = (today.getMonth()+1) + ' ' + today.getDate() + ' ' + (year+"").substring(2,4);
  478.                     if (new Date(PhProxy_prefs.read_str('lastUpdate')) < new Date(ymd)) {
  479.                         PhProxy_prefs.write_str('lastUpdate',ymd);
  480.                         return true;
  481.                     }
  482.                     break;
  483.                 case '2':    //case 2 ; update after restart
  484.                     var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
  485.                     var browserEnumerator = wm.getEnumerator("navigator:browser");
  486.                     var num_of_windows = 0;
  487.                     while (browserEnumerator.hasMoreElements()) {
  488.                         browserEnumerator.getNext();
  489.                         num_of_windows++;
  490.                     }
  491.                     return (num_of_windows == 1) ;
  492.                     break;
  493.                 default:    //case 0 ; do not update default server
  494.                     return false;
  495.                 }
  496.             }();
  497.             
  498.             if (is_default_server_update_time) {
  499.                 setTimeout(function(){ PhProxy.default_server._server.initial(); }, 5000 - (force ? 4500 : 0) );
  500.                 setTimeout(function(){
  501.                     var req = new XMLHttpRequest();
  502.                     
  503.                     req.open('GET', PhProxy_prefs.read_str("Server") + "?request=dServer&num=" + num, true);
  504.                     req.channel.loadFlags |= Components.interfaces.nsIRequest.LOAD_BYPASS_CACHE;
  505.                     req.onreadystatechange = function (aEvt) {
  506.                         if (req.readyState == 4){
  507.                             if(req.status == 200)
  508.                                 PhProxy.default_server._server.finished(req.responseText);
  509.                             else
  510.                                 PhProxy.default_server._server.finished(null);
  511.                         }
  512.                     };
  513.                     req.send(null);     
  514.                 }, 10000 - (force ? 4500 : 0));
  515.             }
  516.         }    
  517.     },
  518.     
  519.     update_servers: function() {
  520.         var req = new XMLHttpRequest();
  521.                 
  522.         req.open('GET', PhProxy_prefs.read_str("Server") + "?request=servers", true);
  523.         req.channel.loadFlags |= Components.interfaces.nsIRequest.LOAD_BYPASS_CACHE;
  524.         req.onreadystatechange = function (aEvt) {
  525.             if (req.readyState == 4){
  526.                 if(req.status == 200) {
  527.                     //Try to correct received data even if it contains more lines
  528.                     var sURL=req.responseText.match(/[^\n]*/).toString().split("|");
  529.                     if(!String(sURL[0]).match('InBasic>ServerInUse>PhProxy>Code124587')) 
  530.                         return;
  531.                     PhProxy_prefs.write_str('UpdateVer', sURL[1]);
  532.                     for (var y = 2; y < 18; y += 2) {
  533.                         //update menu list in statusbar
  534.                         var wm =Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
  535.                         var browserEnumerator = wm.getEnumerator("navigator:browser");
  536.                         while(browserEnumerator.hasMoreElements())  //maybe more than 1 window opens!!
  537.                         {  
  538.                           var dc = browserEnumerator.getNext().document;    
  539.                             /*Important note: getElementsByTagName('menuitem')[(y)/2-1].label) doesnt work in this case because window doesnt load yet!*/    
  540.                             //Fist item is dServer
  541.                           dc.getElementById('Ph.Status.PH7.menupopup').getElementsByTagName('menuitem')[(y)/2].setAttribute('label', sURL[y]);
  542.                           dc.getElementById('Ph.Status.PH7.menupopup').getElementsByTagName('menuitem')[(y)/2].setAttribute('value', sURL[y+1]);
  543.                         }
  544.                     }
  545.                     //Update prefs
  546.                     PhProxy_prefs.write_str('sURL',sURL[2]+'|'+sURL[3]+'|'+sURL[4]+'|'+sURL[5]+'|'+sURL[6]+'|'+sURL[7]+'|'+sURL[8]+'|'+sURL[9]+'|'+sURL[10]+'|'+sURL[11]+'|'+sURL[12]+'|'+sURL[13]+'|'+sURL[14]+'|'+sURL[15]+'|'+sURL[16]+'|'+sURL[17]);
  547.                 }            
  548.                 else {}
  549.             }
  550.         };
  551.         req.send(null);
  552.     },
  553.     /*  Webbase functions */
  554.  
  555.     correct_statusbar_text : function(e){            
  556.         var target = e.target;    //To find href corectlly    
  557.         while (target && !("href" in target))
  558.             target = target.parentNode;
  559.         PhProxy_object.mouse_over_href = target ? target.href : "";
  560.         
  561.         //alert(PhProxy_object.mouse_over_href);
  562.         
  563.         if(PhProxy_object.mouse_over_href.match(PhProxy_prefs.read_str('curURL')))
  564.         {    
  565.             if(PhProxy_prefs.read_str('infoShow')=='1')
  566.             {
  567.                 var sBarDisp = document.getElementById("Ph.Status.ExternalTip");
  568.                 
  569.                 sBarDisp.label = PhProxy_url_creator.depoxify(PhProxy_object.mouse_over_href);
  570.                 
  571.                 if(String(sBarDisp.label).length>30){
  572.                     sBarDisp.label=String(sBarDisp.label).substr(0,25)+'...'+String(sBarDisp.label).substr(String(sBarDisp.label).length-22,22);
  573.                 }
  574.                 sBarDisp.showPopup( PhProxy_object.statusbar_id, -1, -1, 'tooltip', 'bottomleft', 'bottomright');
  575.             }
  576.             else if(PhProxy_prefs.read_str('infoShow')=='0')
  577.             {
  578.                 document.getElementById("statusbar-display").label ='PH::  ' + PhProxy_url_creator.depoxify(PhProxy_object.mouse_over_href); 
  579.                 e.preventDefault(); 
  580.             }
  581.         }
  582.     },
  583.     
  584.     correct_statusbar_text_distroy : function(e){
  585.         //hide popup for mode 2
  586.         var sBarDisp = document.getElementById("Ph.Status.ExternalTip");
  587.         sBarDisp.hidePopup();
  588.         
  589.         PhProxy_object.mouse_over_href=null;        
  590.     },    
  591.     
  592.     copy_decode_url_to_clipboard : function(e){
  593.     
  594.         if(typeof(e)=="object"){        
  595.             if( !( e.charCode==99 && e.ctrlKey && PhProxy_object.mouse_over_href.match(PhProxy_prefs.read_str('curURL'))) ) return;
  596.             var copytext = PhProxy_object.mouse_over_href;
  597.         }
  598.         else
  599.             var copytext = e;    
  600.         //Copy to clipboard start
  601.         var str     = Components.classes["@mozilla.org/supports-string;1"].
  602.                          createInstance(Components.interfaces.nsISupportsString);
  603.         if (!str){
  604.             alert("clipboard copy problem!");
  605.             return ;
  606.         }
  607.         str.data    = PhProxy_url_creator.depoxify(copytext);
  608.         var trans = Components.classes["@mozilla.org/widget/transferable;1"].
  609.                                createInstance(Components.interfaces.nsITransferable);
  610.         if (!trans){
  611.             alert("clipboard copy problem!");
  612.             return ;
  613.         }
  614.  
  615.         trans.addDataFlavor("text/unicode");
  616.         trans.setTransferData("text/unicode", str, copytext.length * 2);
  617.  
  618.         var clipid = Components.interfaces.nsIClipboard;
  619.         var clip     = Components.classes["@mozilla.org/widget/clipboard;1"].getService(clipid);
  620.         if (!clip) return false;
  621.  
  622.         clip.setData(trans, null, clipid.kGlobalClipboard);
  623.         //Copy to clipboard end
  624.         var copyStr = PhProxy_url_creator.depoxify(copytext);
  625.         copyStr = (copyStr.length > 43) ?  copyStr.substr(0,20) + '...' + copyStr.substr(copyStr.length-20,20) : copyStr;
  626.         alert(stringsBundle.getString('Alert1') +' ' + copyStr + '\n' + stringsBundle.getString('Alert3'));
  627.     },
  628.     //New features on 3.0.3K
  629.     is_this_vaid_phproxy_server: function (link)
  630.     {
  631.         //first test if server is valid PhProxy server then test if server works on index.php or not
  632.         return /aHR0cDovL|d3d3L/.test(this.fix_url(link))
  633.             && (/\/[^\/\?]*\?/.exec(this.fix_url(link))=="/" + PhProxy_prefs.read_str('phpIndex') + "?");
  634.     },
  635.     //This function is grabed from UrlbarExt 1.4.8(https://addons.mozilla.org/en-US/firefox/addon/8758)
  636.     fix_url: function (aURL)
  637.     {
  638.         var d = decodeURIComponent(aURL);
  639.         if (/\%[a-fA-F0-9]{2}/g.test(d)) {
  640.             d = decodeURIComponent(d);
  641.         }
  642.         return d;
  643.     },
  644.     show_notification_on_unknown_servers: function (aURL){
  645.         document.getElementById("Ph.Status.add-new-server-label").hidden = false;
  646.         this.unkonwn_server = aURL.replace(/\/[^\/]*$/,"") + "/";
  647.     },
  648.     hide_notification_on_unknown_servers: function () {
  649.         document.getElementById("Ph.Status.add-new-server-label").hidden = true;        
  650.         this.unkonwn_server = null;
  651.     },
  652.     
  653.     open_secure : function (data) {
  654.         var data = PhProxy_url_creator.proxify.apply_filter(data, "proxify");
  655.         if( data ) 
  656.             PhProxy.open_in.current_tab(data);
  657.     },
  658.     
  659.     open_secure_in_new_tab : function (data) {
  660.         var data = PhProxy_url_creator.proxify.apply_filter(data, "proxify");
  661.         if( data ) 
  662.             PhProxy.open_in.new_tab(data);
  663.     }    
  664. };
  665.  
  666. var PhProxy_windows = {
  667.     stringsBundle :  null,
  668.     
  669.     initial : function(){
  670.          stringsBundle=document.getElementById("Ph.Main.Bundle");
  671.     },
  672.     
  673.     //Bookmarks
  674.     bookmarks_context_menu : function (index) {                  
  675.         var bmsvc = Components.classes["@mozilla.org/browser/nav-bookmarks-service;1"]
  676.                               .getService(Components.interfaces.nsINavBookmarksService);                
  677.         var nodeItemId=PlacesUtils.getConcreteItemId(document.popupNode.node);              
  678.         var nodeURI=bmsvc.getBookmarkURI(nodeItemId).spec;
  679.         index == 1 ? PhProxy.open_secure(nodeURI) : PhProxy.open_secure_in_new_tab(nodeURI);
  680.     },
  681.     
  682.     //Statusbar
  683.     change_icon_color : function(color){
  684.         if(!(content.document.location.href).match(PhProxy_prefs.read_str('curURL')))
  685.             document.getElementById("Ph-Status-Icon").setAttribute('status', color);
  686.     },
  687.  
  688.     onleftclick : function(event){
  689.         if (event.button==0)
  690.             if (PhProxy.unkonwn_server) {
  691.                 PhProxy_prefs.write_str("curURL", PhProxy.unkonwn_server);
  692.                 PhProxy_prefs.write_str("PrType", "1");
  693.                 //Alert for new changes
  694.                 alert(stringsBundle.getString('Alert11') + " "
  695.                       + PhProxy.unkonwn_server + "\n"
  696.                       + stringsBundle.getString('Alert12'));
  697.                 //Must place after all parts which need PhProxy.unkonwn_server
  698.                 //Remove notification label
  699.                 PhProxy.hide_notification_on_unknown_servers(this.selected_page_href);
  700.                 //force to process new URL
  701.                 var aURL = {spec:null}; //Generate a vertual aURL
  702.                 aURL.spec = content.document.location.href;
  703.                 PhProxy.process_new_URL(aURL);
  704.             }
  705.             else
  706.                 this.context(parseInt(PhProxy_prefs.read_str('LeftClick')));
  707.     },
  708.     
  709.     show_alert_in_statusbar : function(msg){    
  710.         document.getElementById('Ph.Status.Label').value = document.getElementById("Ph.Main.Bundle").getString(msg);
  711.         document.getElementById('Ph.Status.Label').hidden=false;
  712.         setTimeout(function() {
  713.             document.getElementById('Ph.Status.Label').hidden=true;
  714.         }, 3000);    
  715.     },    
  716.     
  717.     context : function(index, extra_index){    
  718.         const oURL = content.document.location.href;
  719.         switch(index){
  720.             case 0:            
  721.                 const sURL=PhProxy_prefs.read_str('curURL');
  722.                 if(oURL.match(sURL))
  723.                     alert(stringsBundle.getString('Alert2'));
  724.                 else{
  725.                     data = PhProxy_url_creator.proxify.apply_filter(oURL, "proxify");
  726.                     if( data )
  727.                         PhProxy.open_in.current_tab(data);
  728.                 }                    
  729.                 break;        
  730.             case 3:
  731.                 window.openDialog('chrome://inbasicph/content/Options.xul','Ph.Option.Window',
  732.                                   'toolbar, centerscreen, chrome, resizable').focus();
  733.                 break;
  734.             case 4:
  735.                 window.openDialog('chrome://inbasicph/content/About.xul',
  736.                                   'Ph.About.Window','centerscreen, chrome, resizable').focus();
  737.                 break;
  738.             case 5:
  739.                 switch (extra_index) {
  740.                     case 0:  //Changing privacy type
  741.                     case 1:
  742.                     case 2:
  743.                         PhProxy_prefs.write_str('privacyLevel', extra_index.toString() );
  744.                         break;
  745.                     default:    //Changing cache type
  746.                         PhProxy_prefs.write_str('cacheType', (extra_index-3).toString() );
  747.                 }
  748.                 //update menu for all windows
  749.                 var wm =Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
  750.                 var browserEnumerator = wm.getEnumerator("navigator:browser");
  751.                 while(browserEnumerator.hasMoreElements())  //maybe more than 1 window opens!!
  752.                 {  
  753.                     var dc = browserEnumerator.getNext().document;    
  754.                     //Statusbar radio boxs
  755.                     dc.getElementById('Ph-Status-PH8-RadioG1').childNodes[parseInt(PhProxy_prefs.read_str('privacyLevel'))].setAttribute("checked", true);
  756.                     dc.getElementById('Ph-Status-PH8-RadioG2').childNodes[parseInt(PhProxy_prefs.read_str('cacheType'))].setAttribute("checked", true);
  757.                 }                
  758.                 break;
  759.             case 6:
  760.                 //update default server
  761.                 PhProxy_object.default_server.update(true, extra_index);
  762.                 break;
  763.             default:
  764.                 /* Read Clipboard */
  765.                 var clip    = Components.classes["@mozilla.org/widget/clipboard;1"].getService(Components.interfaces.nsIClipboard);
  766.                 if (!clip){
  767.                     alert("Clipboard copy problem!");
  768.                     return ;
  769.                 }
  770.                 var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
  771.                 if (!trans){
  772.                     alert("Clipboard copy problem!");
  773.                     return ;
  774.                 }
  775.                 trans.addDataFlavor("text/unicode");
  776.                 clip.getData(trans, clip.kGlobalClipboard);
  777.                 var str             = new Object();
  778.                 var strLength    = new Object();
  779.                 trans.getTransferData("text/unicode", str, strLength);
  780.                 if (str) str = str.value.QueryInterface(Components.interfaces.nsISupportsString);
  781.                 if (str) pastetext = str.data.substring(0, strLength.value / 2);
  782.                 /* End Read Clipboard */        
  783.                 if(pastetext){
  784.                     if (index==1) PhProxy.open_secure(pastetext);
  785.                     else          PhProxy.open_secure_in_new_tab(pastetext);
  786.                 }
  787.                 else
  788.                     alert(stringsBundle.getString('Alert4'));
  789.         }
  790.     }    
  791. };
  792.  
  793. /**Read Write Pref */
  794. var PhProxy_prefs  = {
  795.   prefs: Components.classes["@mozilla.org/preferences-service;1"]
  796.       .getService(Components.interfaces.nsIPrefService).getBranch("InBasic.PhProxy."),
  797.   
  798.   read_str : function (id) {
  799.     return this.prefs.getCharPref(id);
  800.   },  
  801.   write_str : function (id, sData) {
  802.     this.prefs.setCharPref(id, sData);
  803.   },
  804.   read_bool : function (id) {
  805.     return this.prefs.getBoolPref(id);
  806.   },  
  807.   write_bool : function (id, sData) {
  808.     this.prefs.setBoolPref(id, sData);
  809.   }
  810. }
  811.  
  812. /*************************************************************************Window phv_servers_listener *****************************************************/
  813. PhProxy_object = PhProxy;
  814. window.addEventListener("load", function() {PhProxy_object.initial()}, false);
  815. window.addEventListener("unload", function() {PhProxy_object.terminate()}, false);